package com.example.controller;

import com.example.bean.crm.ckb;
import com.example.bean.crm.fhb;
import com.example.service.crmService.crmService;
import com.example.service.k3Service.k3Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

/**
 * 发货模块
 * Created by tony on 2017/12/15.
 */
@Component
@RestController
public class fhControl {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //定义k3Service
    @Autowired
    private k3Service k3Service;
    //定义mesService
    @Autowired
    private crmService crmService;

    /**
     * 发货通知更新
     */
    @RequestMapping("/insertFh")
    @Scheduled(cron = "0 0/5 * * * ?") //每5执行一次
    public void insertFhScheduled() {
        logger.info("发货更新。开始……");

        //取得更新的销售出库
        ArrayList<fhb> fhlist = k3Service.selectFh();
        for (fhb fhb : fhlist) {
            try {
                int CRMclyf = fhb.getCRMclyf();
                if (CRMclyf == 1) {
                    //中间表中插入发货通知
                    if (fhb.getStatus() == 0) {
                        crmService.insertFh(fhb);
                        //标示已经传入中间表
                        k3Service.updateFh(fhb.getFhdID());
                    } else {
                        //更新中间表
                        crmService.updateFh(fhb);
                    }
                    //变更同一采购明细下已经变2的数据，状态变1
                    crmService.updateFhFlg(fhb);
                } else {
                    //反审核且数据没传如中间表
                    if (fhb.getStatus() > 0) {
                        crmService.updateFhFlg(fhb);
                        crmService.updateFh(fhb);
                    }
                }
            } catch (Exception e) {
                logger.info(e.getMessage());
            }
        }

        logger.info("发货更新。结束！");
    }

    /**
     * 发货通知删除检查
     */
    @RequestMapping("/deleteFh")
    @Scheduled(cron = "0 0/5 * * * ?") //每5执行一次
    public void deleteFhScheduled() {
        logger.info("发货通知删除检查。开始……");

        //取得更新的销售出库
        ArrayList<String> fhlist = k3Service.selectDeFh();
        for (String FDescription : fhlist) {
            try {
                String fhdID = FDescription.substring(FDescription.indexOf("[") + 1, FDescription.lastIndexOf("]"));
                //获取采购订单明细id
                Map<String, String> map = crmService.selectFhInfoamation(fhdID);
                if (map != null) {
                    crmService.updateDeFhFlg(fhdID);
                    crmService.updateDeFhFlg2(map.get("cgddmxID"));
                    logger.info(fhdID + "被发货通知删除");
                }else {
                    logger.info(fhdID + "不是crm传入订单");
                }

            } catch (Exception e) {
                logger.info(e.getMessage());
            }
        }

        logger.info("发货通知删除检查。结束！");
    }

}
